﻿using Oddo.Concept.Crm.Common.Communication.Security;

namespace Oddo.Concept.Crm.Server.Repository.Controller
{
    using System;
    using System.Web.Http;
    using Microsoft.Practices.Unity;
    using Oddo.Concept.Crm.Common.Communication.Controllers;
    using Oddo.Concept.Crm.Repository.Common.Contracts;
    using System.Net.Http;

    public interface IRepositoryController
    {
        CountryFindResponse FindCountries(CountryFindRequest request);
    }

    [BasicHttpAutorize]
    public class RepositoryServicesController : ApiControllerBase, IRepositoryController
    {
        [Dependency]
        public IRepositoryServices RepositoryServices { get; set; }

        public override void RegisterRoutes(HttpRouteCollection routes)
        {
            routes.MapHttpRoute(name: "repository",
                                routeTemplate: "api/repository/{action}/",
                                defaults: new { controller = "RepositoryServices" });
        }

        //[HttpGet]
        [HttpPost]
        [ActionName("Countries")]
        public CountryFindResponse FindCountries(CountryFindRequest request)
        {
            if (!this.User.Identity.IsAuthenticated)
                throw new UnauthorizedAccessException();
            
            return RepositoryServices.FindCountries(request) as CountryFindResponse;
        }
    }
}